package com.baturu.vin.benz.service.impl.supers;

import com.baturu.vin.benz.dal.dao.BenzMainGroupDAO;
import com.baturu.vin.benz.dto.*;
import com.baturu.vin.benz.service.impl.util.BenzI18nService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * BenzSuperMainGroupService
 * 奔驰超级ecp 主组查询
 *
 * @author majianming
 * @date 2019-04-08 14:52
 */
@Service
public class BenzSuperMainGroupService {
    @Autowired
    private BenzI18nService benzI18nService;

    @Autowired
    private BenzMainGroupDAO benzMainGroupDAO;


    /**
     * 查询主组信息
     *
     * @param superCarInfoDTO 车型解析信息
     * @param wrapperGroup    是否将主组中的发动机信息和变速箱信息包装为拓展属性，如果是<code>true</code>表示包装为拓展属性
     * @return 主组信息
     */
    List<BenzSuperMainGroupDTO> queryMainGroups(BenzSuperCarInfoDTO superCarInfoDTO, boolean wrapperGroup) {
        if (Objects.isNull(superCarInfoDTO)) {
            return Collections.emptyList();
        }
        BenzSuperCarConfigurationDTO carConfigurationDTO = superCarInfoDTO.getBenzSuperCarConfigurationDTO();
        if (Objects.isNull(carConfigurationDTO)) {
            return Collections.emptyList();
        }
        Map<String, BenzPartCatInfoDTO> catInfoMap = carConfigurationDTO.getCatInfoMap();
        if (MapUtils.isEmpty(catInfoMap)) {
            return Collections.emptyList();
        }
        List<String> carNos = catInfoMap.values().stream().map(BenzPartCatInfoDTO::getCatalog).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(carNos)) {
            return Collections.emptyList();
        }

        List<BenzSuperMainGroupDTO> benzSuperMainGroupDTOS = benzMainGroupDAO.queryMainGroup(carNos);
        if (CollectionUtils.isEmpty(benzSuperMainGroupDTOS)) {
            return Collections.emptyList();
        }
        Set<String> descIndexSet = benzSuperMainGroupDTOS.stream().map(BenzSuperMainGroupDTO::getDescIndex).collect(Collectors.toSet());
        Map<String, List<BenzSuperDescDTO>> groupDescMap = benzMainGroupDAO.queryMainGroupDesc(descIndexSet, benzI18nService.getBenzLangEnum().getLandCode()).stream().collect(Collectors.groupingBy(BenzSuperDescDTO::getDescIndex));

        benzSuperMainGroupDTOS.forEach(l -> {
            String descIndex = l.getDescIndex();
            List<BenzSuperDescDTO> benzSuperDescDTOS = groupDescMap.get(descIndex);
            if (CollectionUtils.isNotEmpty(benzSuperDescDTOS)) {
                l.setDesc(benzSuperDescDTOS.get(0).getDesc());
                l.setShowName(l.getGroupNo() + " " + l.getDesc());
            }
            l.setSortIndex(l.getGroupNo());
            l.setBaseCode(l.getCatNum());
        });

        //如果不包装 那么到这里就可以返回了
        if (!wrapperGroup) {
            return benzSuperMainGroupDTOS;
        }
        return wrapperMainGroupDTOS(carConfigurationDTO, benzSuperMainGroupDTOS);
    }

    /**
     * 包装一下MainGroup
     * 将发动机/变速箱从总的MainGroup抽出作为mianGroup的下级列表 并排序
     *
     * @param carConfigurationDTO
     * @param benzSuperMainGroupDTOS
     * @return
     */
    private List<BenzSuperMainGroupDTO> wrapperMainGroupDTOS(BenzSuperCarConfigurationDTO carConfigurationDTO, List<BenzSuperMainGroupDTO> benzSuperMainGroupDTOS) {
        List<BenzSuperMainGroupDTO> benzCarMainGroupDTOS = benzSuperMainGroupDTOS.stream().filter(m -> StringUtils.equals(m.getCatNum(), carConfigurationDTO.getCarNo())).collect(Collectors.toList());
        List<BenzSuperMainGroupDTO> benzEngineMainGroupDTOS = benzSuperMainGroupDTOS.stream().filter(m -> StringUtils.equals(m.getCatNum(), carConfigurationDTO.getEngineNo())).collect(Collectors.toList());
        List<BenzSuperMainGroupDTO> benzTransmissionMainGroupDTOS = benzSuperMainGroupDTOS.stream().filter(m -> StringUtils.equals(m.getCatNum(), carConfigurationDTO.getTransmissionNo())).collect(Collectors.toList());


        List<BenzModelDTO> modelInfos = carConfigurationDTO.getModelInfos();
//        添加主组的信息
        if (modelInfos.stream().anyMatch(l -> StringUtils.isNotBlank(l.getEngine()))) {
            benzCarMainGroupDTOS.add(buildExtendsGroup("M", benzI18nService.trans("engine"), carConfigurationDTO.getEngineName(), carConfigurationDTO.getEngineNo(), benzEngineMainGroupDTOS));
        }
        if (modelInfos.stream().anyMatch(l -> StringUtils.isNotBlank(l.getManual()))) {
            benzCarMainGroupDTOS.add(buildExtendsGroup("GM", benzI18nService.trans("manual_transmission"), carConfigurationDTO.getTransmissionName(), carConfigurationDTO.getTransmissionNo(), benzTransmissionMainGroupDTOS));
        } else if (modelInfos.stream().anyMatch(l -> StringUtils.isNotBlank(l.getAuto()))) {
            benzCarMainGroupDTOS.add(buildExtendsGroup("GA", benzI18nService.trans("auto_transmission"), carConfigurationDTO.getTransmissionName(), carConfigurationDTO.getTransmissionNo(), benzTransmissionMainGroupDTOS));
        }
        benzCarMainGroupDTOS.sort(Comparator.comparing(BenzSuperMainGroupDTO::getSortIndex));
        return benzCarMainGroupDTOS;
    }

    private BenzSuperMainGroupDTO buildExtendsGroup(String code, String desc, String extendName, String modelNo, List<BenzSuperMainGroupDTO> extendGroups) {
        if (StringUtils.isBlank(extendName)) {
            extendName = "";
        } else {
            extendName = " (" + extendName + ") ";
        }
        extendGroups.forEach(l -> l.setBaseCode(modelNo));
        return BenzSuperMainGroupDTO.builder().hasNext(true)
                .descIndex("0")
                .showName(code + " - " + desc + extendName)
                .groupNo(code)
                .desc(desc)
                .sortIndex("1")
                .hasNext(true)
                .subMainGroupDTOS(extendGroups)
                .build();
    }
}
